<?php
	class News_category extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($name = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
			}else{
				$name = urldecode($name);
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('name' => $name);
			$this->load->model("News_categorys");
			$result = $this->News_categorys->get_news_category_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/news/index/' . $name;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("action", '资讯分类列表');
			$this->smarty->assign("name", $name);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("news_category_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("news_category/list.html");
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				$post_data['add_time'] = time();
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				$this->db->insert("news_categorys", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				$this->load->model("News_categorys");
				$top_news_categorys = $this->News_categorys->get_top_categorys();
				$this->smarty->assign("top_news_categorys", $top_news_categorys);
				$this->smarty->assign("action", '添加资讯分类');
				$this->smarty->assign("post_url", '/news_category/add');
				$this->smarty->display("news_category/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(!isset($post_data['status'])) $post_data['status'] = 0;
				$this->db->where('id', $post_data['id']);
 				$this->db->update('news_categorys', $post_data);
 				if($this->db->affected_rows() > 0){
 					show_message($_SERVER['HTTP_REFERER']);
 				}else{
 					show_message($_SERVER['HTTP_REFERER'], "操作失败");
 				}
			}else{
				if(intval($id) > 0){
					$news_category_info = get_field_by_where("news_categorys", "*", "id='{$id}'");
					if(!$news_category_info){
						show_message("/news/type_list", "数据不存在");exit;
					}
					$this->load->model("News_categorys");
					$top_news_categorys = $this->News_categorys->get_top_categorys();
					$this->smarty->assign("top_news_categorys", $top_news_categorys);
					$this->smarty->assign("news_category_info", $news_category_info);
					$this->smarty->assign("action", '编辑资讯分类');
					$this->smarty->assign("post_url", '/news_category/edit');
					$this->smarty->display("news_category/save.html");
				}else{
					show_message("/news_category", "非法操作");
				}
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "news_categorys");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}
	}
